package ro.gillardon.beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;

import ro.gillardon.business.priceservice.PlanePriceService;
import ro.gillardon.business.priceservice.impl.plane.PlanePriceServiceEJB;
import ro.gillardon.commons.dto.PlaneDTO;
import ro.gillardon.commons.logger.BTPLogger;
import ro.gillardon.commons.mot.plane.PlaneConditions;
import ro.gillardon.commons.util.DateUtils;

@ManagedBean(name = "planeView", eager = true)
@SessionScoped
public class PlaneView {

  private static final Logger LOG = Logger.getLogger(PlaneView.class);
  private PlaneConditions planeConditions;
  private List<PlaneDTO> planeDTOList;
  private String printTest;

  @ManagedProperty("#{locationView.fromSelected}")
  private String locationsFrom;
  @ManagedProperty("#{locationView.toSelected}")
  private String locationsTo;
  @ManagedProperty("#{calendarView.fromDate}")
  private Date calendarFrom;
  @ManagedProperty("#{calendarView.toDate}")
  private Date calendarTo;

  @EJB
  private final PlanePriceService planeService = (PlanePriceService) BeanProvider.getEJB(PlanePriceServiceEJB.class
    .getSimpleName());

  public PlaneView() {
    // default constructor
  }

  @PostConstruct
  public void init() {
    this.planeDTOList = new ArrayList<PlaneDTO>();
  }

  public void populateOfferTable() {

    /*
     * FacesContext context = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest)
     * context.getExternalContext().getRequest(); HttpSession httpSession = request.getSession(false);
     *
     * Date toDate = (Date) httpSession.getAttribute("#{calendarView}.toDate"); Date fromDate = (Date)
     * httpSession.getAttribute("#{calendarView}.fromDate"); String fromLocationSelected = (String)
     * httpSession.getAttribute("#{locationView}.fromSelected"); String toLocationSelected = (String)
     * httpSession.getAttribute("#{locationView}.toSelected");
     */

    this.printTest =
      "toDate: " + this.calendarTo.toString() + ", fromDate: " + this.calendarFrom.toString() +
      ", fromLocation: " + this.locationsFrom + ", toLocataion :" + this.locationsTo;

    /*
     * "toDate: " + toDate + ", fromDate: " + fromDate + ", fromLocation: " + fromLocationSelected + ", toLocataion :" +
     * toLocationSelected;
     */

    BTPLogger.error(LOG, this.printTest);

  }

  public PlaneConditions getPlaneConditions() {
    return this.planeConditions;
  }

  public void setPlaneConditions(final PlaneConditions planeConditions) {
    this.planeConditions = planeConditions;
  }

  public List<PlaneDTO> getPlaneDTOList() {
    return this.planeDTOList;
  }

  public void setPlaneDTOList(final List<PlaneDTO> planeDTOList) {
    this.planeDTOList = planeDTOList;
  }

  public String getPrintTest() {

    return this.printTest;
  }

  public void setPrintTest(final String printTest) {
    this.printTest = printTest;
  }

  public String getLocationsFrom() {
    return this.locationsFrom;
  }

  public void setLocationsFrom(final String locationsFrom) {
    this.locationsFrom = locationsFrom;
  }

  public String getLocationsTo() {
    return this.locationsTo;
  }

  public void setLocationsTo(final String locationsTo) {
    this.locationsTo = locationsTo;
  }

  public Date getCalendarFrom() {
    return DateUtils.createNewDate(this.calendarFrom);
  }

  public void setCalendarFrom(final Date calendarFrom) {
    this.calendarFrom = DateUtils.createNewDate(calendarFrom);
  }

  public Date getCalendarTo() {
    return DateUtils.createNewDate(this.calendarTo);
  }

  public void setCalendarTo(final Date calendarTo) {
    this.calendarTo = DateUtils.createNewDate(calendarTo);
  }

}
